home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / ToolBarPanel.java < prev    next >
Text File  |  1998-08-21  |  14KB  |  428 lines

  1. package symantec.itools.awt.util;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Rectangle;
  8. import java.awt.FlowLayout;
  9. import java.awt.LayoutManager;
  10. import symantec.itools.awt.BorderPanel;
  11. import symantec.itools.awt.BevelStyle;
  12. import java.beans.PropertyVetoException;
  13. import java.beans.PropertyChangeEvent;
  14. import java.beans.VetoableChangeListener;
  15. import java.beans.PropertyChangeListener;
  16. import java.util.ResourceBundle;
  17.  
  18. // 01/29/97    TWB    Integrated changes from Windows
  19. // 05/22/97 RKM    Removed call to super to call getComponents in preferredSize
  20. //                Java 1.1 compiler does not like this
  21. // 06/03/97 TGL    Converted to 1.1, added bound/constrained
  22. // 07/23/97 LAB    Updated preferredSize to getPreferredSize and eliminated minimumSize since
  23. //                BorderPanel already overrides it to call getPreferredSize.  Added Orientaion
  24. //                property to allow the toolbar's getPreferredSize to return a Dimenstion that
  25. //                is best suited for a vertical orientation.  Added a new constructor with
  26. //                parameters.  Constrained the Orientation property to known Orientations.
  27. //                Updated version to 1.1.  Added initialization to the default constructor
  28. //                (it was missing initialization before).
  29. // 08/13/97 LAB    Added a check in setOrientation() to see if the panel contains any components,
  30. //                if not then the call does not result in a reshape.  Addresses Mac Bug #7253.
  31. // 10/01/97 LAB    Made getPreferredSize() independent from the base class to make sure the
  32. //                size is always calculated correctly, regardless of changes to the base class.
  33. //                This was affecting the HORIZONTAL orientation (Addresses Mac Bug #6987).
  34. //                Changed names of strings in PropertyChangeEvent handling to follow Bean Spec
  35. //                naming conventions.
  36.  
  37. /**
  38.  * ToolBarPanel component.
  39.  * This component creates a panel to which you can add buttons to create a toolbar
  40.  * in a window. Toolbars commonly contain buttons, but a ToolBarPanel can hold
  41.  * other types of components like static text, check boxes, even images.
  42.  *
  43.  * Tool bar components are separated with a ToolBarSpacer component.
  44.  *
  45.  * @see symantec.itools.awt.util.ToolBarSpacer
  46.  *
  47.  * @version 1.1, July 23, 1997
  48.  * @author Symantec
  49.  */
  50. public class ToolBarPanel extends BorderPanel
  51. {
  52.     /**
  53.      * The constant to use to have a horizontally oriented toolbar.
  54.      * @see #setOrientation
  55.      * @see #getOrientation
  56.      */
  57.     public static final int HORIZONTAL    = 0;
  58.  
  59.     /**
  60.      * The constant to use to have a vertically oriented toolbar.
  61.      * @see #setOrientation
  62.      * @see #getOrientation
  63.      */
  64.     public static final int VERTICAL        = 1;
  65.  
  66.     /**
  67.      * Create ToolBarPanel.  Default ToolBarPanel uses a BEVEL_RAISED style and is
  68.      * HORIZONTAL.
  69.      */
  70.     public ToolBarPanel()
  71.     {
  72.         padleft     = 0;
  73.         padright    = 0;
  74.         padtop      = 0;
  75.         padbottom   = 0;
  76.         ixPad       = 4;
  77.         iyPadTop    = 4;
  78.         iyPadBottom = 3;
  79.  
  80.         super.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
  81.         try
  82.         {
  83.             setBevelStyle(BevelStyle.BEVEL_RAISED);
  84.             setOrientation(HORIZONTAL);
  85.         }
  86.         catch(PropertyVetoException veto) {};
  87.     }
  88.  
  89.     /**
  90.      * Create ToolBarPanel.
  91.      * @param bevelStyle either BEVEL_RAISED, BEVEL_LOWERED, BEVEL_LINE, or BEVEL_NONE.
  92.      * If the value passed is not valid, it will default to BEVEL_RAISED.
  93.      * @param orientationType either HORIZONTAL or VERTICAL.  If the value passed is not
  94.      * valid, it will default to HORIZONTAL.
  95.      * @see BevelStyle#BEVEL_RAISED
  96.      * @see BevelStyle#BEVEL_LOWERED
  97.      * @see BevelStyle#BEVEL_LINE
  98.      * @see BevelStyle#BEVEL_NONE
  99.      * @see #HORIZONTAL
  100.      * @see #VERTICAL
  101.      */
  102.     public ToolBarPanel(int bevelStyle, int orientationType)
  103.     {
  104.         this();
  105.  
  106.         try
  107.         {
  108.             setBevelStyle(bevelStyle);
  109.         }
  110.         catch (PropertyVetoException exc) {}
  111.         try
  112.         {
  113.             setOrientation(orientationType);
  114.         }
  115.         catch (PropertyVetoException exc) {}
  116.     }
  117.  
  118.     /**
  119.      * Sets the orientation of the toolbar.  Modifies the results from getPreferredSize.
  120.      * If the orientation is VERTICAL, getPreferredSize will return the Dimension
  121.      * best suited for this toolbar layed out vertically.  If the orientaion
  122.      * is HORIZONTAL getPreferredSize will return the Dimension best suited for this
  123.      * toolbar layed out horizontally.
  124.      * @param orientationType the orientation to adhere to.  Either HORIZONTAL or VERTICAL
  125.      * @exception PropertyVetoException
  126.      * if the specified property value is unacceptable
  127.      * @see #getOrientation
  128.      * @see #HORIZONTAL
  129.      * @see #VERTICAL
  130.      */
  131.     public void setOrientation(int orientationType) throws PropertyVetoException
  132.     {
  133.         if (orientation != orientationType)
  134.         {
  135.             Integer oldValue = new Integer(orientation);
  136.             Integer newValue = new Integer(orientationType);
  137.  
  138.             vetos.fireVetoableChange("orientation", oldValue, newValue);
  139.  
  140.             orientation = orientationType;
  141.  
  142.             FlowLayout temp = (FlowLayout)panel.getLayout();
  143.  
  144.             if (orientation == HORIZONTAL)
  145.             {
  146.                 temp.setAlignment(FlowLayout.LEFT);
  147.             }
  148.             else if (orientation == VERTICAL)
  149.             {
  150.                 temp.setAlignment(FlowLayout.CENTER);
  151.             }
  152.             panel.setLayout(temp);
  153.  
  154.             if(getComponentCount() > 0)
  155.             {
  156.                 setSize(getPreferredSize());
  157.                 validate();
  158.             }
  159.  
  160.             changes.firePropertyChange("orientation", oldValue, newValue);
  161.         }
  162.     }
  163.  
  164.     /**
  165.      * Gets the orientation of the toolbar.
  166.      * @return VERTICAL: getPreferredSize will return the Dimension
  167.      * best suited for this toolbar layed out vertically. HORIZONTAL: getPreferredSize
  168.      * will return the Dimension best suited for this toolbar layed out horizontally.
  169.      * @see #setOrientation
  170.      */
  171.     public int getOrientation()
  172.     {
  173.         return orientation;
  174.     }
  175.  
  176.     /**
  177.      * Takes no action.
  178.      * This is a standard Java AWT method which gets called to specify
  179.      * which layout manager should be used to layout the components in
  180.      * standard containers.
  181.      *
  182.      * Since layout managers CANNOT BE USED with this container the standard
  183.      * setLayout has been OVERRIDDEN for this container and does nothing.
  184.      *
  185.      * @param lm the layout manager to use to layout this container's components
  186.      * (IGNORED)
  187.      * @see java.awt.Container#getLayout
  188.      **/
  189.     public void setLayout(LayoutManager lm)
  190.     {
  191.     }
  192.  
  193.     /**
  194.      * Returns the recommended dimensions to properly display this component.
  195.      * This is a standard Java AWT method which gets called to determine
  196.      * the recommended size of this component.
  197.      *
  198.      * @see symantec.itools.awt.BorderPanel#getMinimumSize
  199.      */
  200.     public synchronized Dimension getPreferredSize()
  201.     {
  202.         Dimension s = new Dimension(0, 0);
  203.         Component[] list = getComponents();
  204.  
  205.         switch(orientation)
  206.         {
  207.             case HORIZONTAL:
  208.                 for (int i = 0; i < list.length; ++i)
  209.                 {
  210.                     Dimension cs = list[i].getSize();
  211.                     s.width += cs.width;
  212.                     s.height = Math.max(s.height, cs.height);
  213.                 }
  214.                 break;
  215.             case VERTICAL:
  216.                 for (int i = 0; i < list.length; ++i)
  217.                 {
  218.                     Dimension cs = list[i].getSize();
  219.                     s.height += cs.height;
  220.                     s.width = Math.max(s.width, cs.width);
  221.                 }
  222.                 break;
  223.         }
  224.         s.width        += (padleft + padright + ixPad * 2) + 1;
  225.         s.height    += (getLabelTopMargin() + padbottom + iyPadTop + iyPadBottom) + 1;
  226.  
  227.         if (s.width == 0)
  228.             s.width = 50;
  229.  
  230.         if (s.height == 0)
  231.             s.height = 50;
  232.  
  233.         return s;
  234.     }
  235.  
  236.     /**
  237.      * Is the specified orientation type valid?
  238.      * @param orientationType the type to test
  239.      * @return if true then the parameter was equal to either HORIZONTAL or VERTICAL.
  240.      * @see #setOrientation
  241.      * @see #getOrientation
  242.      * @see #HORIZONTAL
  243.      * @see #VERTICAL
  244.      */
  245.     public boolean isValidOrientation(int orientationType)
  246.     {
  247.         switch(orientationType)
  248.         {
  249.             case HORIZONTAL:
  250.             case VERTICAL:
  251.                 return true;
  252.             default:
  253.                 return false;
  254.         }
  255.     }
  256.  
  257.     /**
  258.      * Tells this component that it has been added to a container.
  259.      * This is a standard Java AWT method which gets called by the AWT when
  260.      * this component is added to a container. Typically, it is used to
  261.      * create this component's peer.
  262.      *
  263.      * It has been overridden here to hook-up event listeners.
  264.      *
  265.      * @see #removeNotify
  266.      */
  267.     public synchronized void addNotify()
  268.     {
  269.         super.addNotify();
  270.         errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  271.  
  272.         //Hook up listeners
  273.         if (veto == null)
  274.         {
  275.             veto = new Veto();
  276.             addOrientationListener(veto);
  277.         }
  278.     }
  279.  
  280.     /**
  281.      * Tells this component that it is being removed from a container.
  282.      * This is a standard Java AWT method which gets called by the AWT when
  283.      * this component is removed from a container. Typically, it is used to
  284.      * destroy the peers of this component and all its subcomponents.
  285.      *
  286.      * It has been overridden here to unhook event listeners.
  287.      *
  288.      * @see #addNotify
  289.      */
  290.     public synchronized void removeNotify()
  291.     {
  292.         //Unhook listeners
  293.         if (veto != null)
  294.         {
  295.             removeOrientationListener(veto);
  296.             veto = null;
  297.         }
  298.  
  299.         super.removeNotify();
  300.     }
  301.  
  302.     /**
  303.      * Adds a listener for all event changes.
  304.      * @param listener the listener to add.
  305.      * @see #removePropertyChangeListener
  306.      */
  307.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  308.     {
  309.         super.addPropertyChangeListener(listener);
  310.         changes.addPropertyChangeListener(listener);
  311.     }
  312.  
  313.     /**
  314.      * Removes a listener for all event changes.
  315.      * @param listener the listener to remove
  316.      * @see #addPropertyChangeListener
  317.      */
  318.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  319.     {
  320.         super.removePropertyChangeListener(listener);
  321.         changes.removePropertyChangeListener(listener);
  322.     }
  323.  
  324.     /**
  325.      * Adds a vetoable listener for all event changes.
  326.      * @param listener the listener to add
  327.      * @see #removeVetoableChangeListener
  328.      */
  329.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  330.     {
  331.         super.addVetoableChangeListener(listener);
  332.         vetos.addVetoableChangeListener(listener);
  333.     }
  334.  
  335.     /**
  336.      * Removes a vetoable listener for all event changes.
  337.      * @param listener the listener to remove
  338.      * @see #addVetoableChangeListener
  339.      */
  340.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  341.     {
  342.         super.removeVetoableChangeListener(listener);
  343.         vetos.removeVetoableChangeListener(listener);
  344.     }
  345.  
  346.     /**
  347.      * Adds a listener for Orienation changes.
  348.      * @param listener the listener to add
  349.      * @see #removeOrientationListener
  350.      */
  351.     public synchronized void addOrientationListener(PropertyChangeListener listener)
  352.     {
  353.         changes.addPropertyChangeListener("orientation", listener);
  354.     }
  355.  
  356.     /**
  357.      * Removes a listener for Orienation changes.
  358.      * @param listener the listener to remove
  359.      * @see #addOrientationListener
  360.      */
  361.     public synchronized void removeOrientationListener(PropertyChangeListener listener)
  362.     {
  363.         changes.removePropertyChangeListener("orientation", listener);
  364.     }
  365.  
  366.     /**
  367.      * Adds a vetoable listener for Orienation changes.
  368.      * @param listener the listener to add
  369.      * @see #removeOrientationListener
  370.      */
  371.     public synchronized void addOrientationListener(VetoableChangeListener listener)
  372.     {
  373.         vetos.addVetoableChangeListener("orientation", listener);
  374.     }
  375.  
  376.     /**
  377.      * Removes a vetoable listener for Orienation changes.
  378.      * @param listener the listener to remove
  379.      * @see #addOrientationListener
  380.      */
  381.     public synchronized void removeOrientationListener(VetoableChangeListener listener)
  382.     {
  383.         vetos.removeVetoableChangeListener("orientation", listener);
  384.     }
  385.  
  386.     /**
  387.      * This is the PropertyChangeEvent handling inner class for the constrained Orientation property.
  388.      * Handles vetoing Orientations that are not valid.
  389.      */
  390.     class Veto implements VetoableChangeListener
  391.     {
  392.         /**
  393.          * This method gets called when an attempt to change the constrained Orientation property is made.
  394.          * Ensures the given Orientation is valid.
  395.          *
  396.          * @param     e a <code>PropertyChangeEvent</code> object describing the
  397.          *             event source and the property that has changed
  398.          * @exception PropertyVetoException if the recipient wishes the property
  399.          *              change to be rolled back
  400.          */
  401.         public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException
  402.         {
  403.             int i = ((Integer)e.getNewValue()).intValue();
  404.             if (!isValidOrientation(i))
  405.             {
  406.                 throw new PropertyVetoException(errors.getString("InvalidOrientation") + i, e);
  407.             }
  408.         }
  409.     }
  410.  
  411.     /**
  412.      * The orientation for the toolbar.  Either HORIZONTAL or VERTICAL.
  413.      * @see #HORIZONTAL
  414.      * @see #VERTICAL
  415.      */
  416.     protected int orientation;
  417.  
  418.     /**
  419.      * Error strings.
  420.      */
  421.     transient protected ResourceBundle errors;
  422.  
  423.     private Veto veto = null;
  424.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  425.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  426. }
  427.  
  428.